<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/24
 * Time: 11:06
 */
namespace app\api\controller;
use app\common\controller\ApiBase;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Event;
use think\facade\Filesystem;
use think\facade\Validate;
use think\Log;

class Indexapi extends ApiBase
{
    private $appid = 'wxc1c600cb9953ca42';
    private $appSecrct = 'f232aec643f584202cf647caa741a32e';

    //获取员工码
    public function getCode(){
        $channel = input('get.channel');
        $cid = Db::name('channel')->where('code',$channel)->value('id');

        if(!$cid){
            return json_encode(['code'=>0,'msg' => '没有找到该渠道码，请联系管理员']);
        }

        $count = Db::name('employee')->where('channelid',$cid)->count('id');

        return json_encode(['code'=>1,'data' =>['channelid'=>$cid,'code'=> $channel.($count+1)]]);
    }

    //获取员工码
    public function bindCode(){
        $code = input('post.code');
        $uid = input('post.uid');
        $channelid = input('post.channelid');
        $aid = Db::name('channel')->where('id',$channelid)->value('aid');

        if(Db::name('employee')->where('id',$uid)->update(['code'=>$code,'channelid'=>$channelid,'update_time'=>time(),'aid'=>$aid])){
            return json_encode(['code'=>1,'data'=>$code]);
        }else{
            return json_encode(['code'=>0,'msg'=>'系统错误']);
        }
    }

    //更新手机号码
    public function addPhone(){
        $iv = input('post.iv');
        $encryptedData = input('post.encryptedData');
        $uid = input('post.uid');
        $getSessionKey = Db::name('employee')->where('id',$uid)->value('session_key');
        $dataObj = decryptData($getSessionKey,$encryptedData,$iv);
        if( $dataObj  == 'IllegalBuffer') return json_encode(['code'=>0,'msg'=>'数据解析错误,请刷新重试']);

        if(Db::name('employee')->where('id',$uid)->update(['phone'=>$dataObj['phoneNumber'],'update_time'=>time()])){
            return json_encode(['code'=>1]);
        }else{
            return json_encode(['code'=>0,'msg'=>'系统错误']);
        }
    }

    //获取帮助信息
    public function getHelp(){
        $data = Db::name('content')->value('content');
        return json_encode(['code'=>1,'data'=>$data]);
    }

    //上传图片
    public function uploadPic(){
        $file = request()->file('file');

        $validate = Validate::rule(['image'=>'filesize:30240|fileExt:jpg,jpeg,png']);
        if(!$validate->check(['file'=>$file])){
            return json_encode(['code'=>0,'msg'=>$validate->getError()]);
        }else{
            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);

            \think\facade\Log::record($savename,'info');
            return json_encode(['code'=>1,'path'=>$savename,'msg'=>'']);
        }
    }

    //获取保单
    public function getBill(){
        $uid = input('get.uid');
        $startTime = input('get.startTime');
        $endTime = input('get.endTime');


        if($startTime == ''){
            $startTime = time();
        }else{
            $startTime = strtotime($startTime);
        }

        if($endTime == ''){
            $endTime = time();
        }else{
            $endTime = strtotime($endTime);
        }

        $map = [['eid','=',$uid],['create_time','between',[$startTime,$endTime]]];

        $data = Db::name('insurance')->where($map)->order('create_time desc')->field('engineCode,ownername,plate,isPay,id')->select();

        $isnotPay = $isPay = [];

        foreach($data as $k => $v){
            if($v['isPay'] == 0){
                array_push($isnotPay,$v);
            }else{
                array_push($isPay,$v);
            }
        }

        return json_encode(['code'=>1,'data'=>$data,'isPay'=>$isPay,'isnotPay'=>$isnotPay]);
    }

}